import router from '@/router'
import type { menu } from '@/types/user'
import { loadComponent } from '@/utils/components'
export const addDynamicsRouter = (menus: menu[]) => {
  menus.forEach(item => {
    if (item.children) {
      item.children.forEach(subitem => {
        subitem.component = loadComponent(subitem.component)
      })
    }
  })
  // console.log('----menus----', menus)
  const oldRouter = router.getRoutes()
  oldRouter.forEach(route => {
    if (!route.meta.isStatic) {
      //删除之前的动态路由
      router.removeRoute(route.name)
    }
  })
  menus.forEach(item => {
    // 添加新的动态路由到layout下面
    router.addRoute('layout', item)
  })
  // console.log(router.getRoutes())
}
